//
//  LXCarStockListViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2022/7/8.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXCarStockListViewController.h"
#import "LXCarListTableViewCell.h"
#import "LXCarStockDetailsViewController.h"
@interface LXCarStockListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)  LXTopSearchView *searchView;
@property (nonatomic,strong)  UITableView       *tableView;
@property (nonatomic, strong) NSMutableArray    *data;
@property (nonatomic, assign) NSInteger          page;
@property (nonatomic, copy) NSString             *searchKey;
@end
#define lineWidth 17
@implementation LXCarStockListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 从搜索进入
    if (self.isSearch) {
        
        [self layoutCustomNav:self.view title:@"车型库存"];
        
        LXTopSearchView *searchView = [[LXTopSearchView alloc] initWithFrame:CGRectMake(0,kViewTopHeight, kScreenWidth, 70)];
        searchView.textField.placeholder = @"请输入品牌/型号进行搜索";
        [searchView.textField becomeFirstResponder];
        [self.view addSubview:searchView];
        self.searchView = searchView;
        // 点击
        [searchView setClickBtnBlock:^(NSString * _Nonnull text) {
            [self.view endEditing:YES];
            self.searchKey = text;
            self.page = 1;
            [self loadData];
        }];
        [self isHaveEmptyData];
        
    }
    
    self.data = [NSMutableArray array];
    
    self.view.backgroundColor = White_Color;
    
    [self.view addSubview:self.tableView];
       
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self loadData];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        self.page++;
        [self loadData];
    }];
  
    if (!self.isSearch) {
        [self.tableView.mj_header beginRefreshing];
    }
    [[kNotifyCenter rac_addObserverForName:@"reloadStockData" object:nil] subscribeNext:^(NSNotification * _Nullable note) {
        [self.tableView.mj_header beginRefreshing];
    }];
}

-(void)loadData{

    NSMutableDictionary *dic = @{
        @"page":@(self.page),
        @"rows":@(10),
        @"type":@(self.type)
    }.mutableCopy;
 
    if (!StringIsEmpty(self.searchKey)) {
        [dic setValue:self.searchKey forKey:@"searchKey"];
    }
    
    
    [MSNetworkManager getRevertUrl:[DOMAIN_URL stringByAppendingString:GET_GetCatTypeList] params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        [self endRefreshing];
        if (isSuccess) {
            
            if ([self.tableView.mj_header isRefreshing]||self.page==1) {
                [self.data removeAllObjects];
                self.tableView.mj_footer.state = MJRefreshStateIdle;
            }
            
            NSInteger totalPage = [resonseObj[@"total"] intValue];
            
            NSArray *records = resonseObj[@"records"];
            [self.data addObjectsFromArray:[CheLiangModel mj_objectArrayWithKeyValuesArray:records]];
            
            if (self.data.count>=totalPage) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                self.tableView.mj_footer.state = MJRefreshStateIdle;
            }
            
            [self.tableView reloadData];
            [self isHaveEmptyData];
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:YES];
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.data.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *identifier = @"cell2";
    LXCarListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([LXCarListTableViewCell class]) owner:self options:nil] objectAtIndex:1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CheLiangModel *model = self.data[indexPath.row];
    cell.stockModel = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CheLiangModel *model = self.data[indexPath.row];
    LXCarStockDetailsViewController *vc = [[LXCarStockDetailsViewController alloc] init];
    vc.ID = model.ID;
    [self pushToNextController:vc];
    
    
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        if (self.isSearch) {
            _tableView.frame = CGRectMake(0, self.searchView.Y_height, ScreenWidth, kScreenHeight - self.searchView.Y_height);
        }
        else {
            _tableView.frame = CGRectMake(0, self.searchView.Y_height, ScreenWidth, kScreenHeight - 90 - kViewTopHeight);
        }
        _tableView.backgroundColor = White_Color;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 95;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}
- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}

@end
